<?php
/**
 * 在玩用户统计
 * crontab每N分钟执行一次，在 /etc/crotab 中加入：
 * * /5 *   *   *   *   root    /usr/local/bin/php -c /data/app/php/etc/php.ini /data/majiang/default/majiang/script.php request/PlayingUsersHandler
 */
class PlayingUsersHandler extends ScriptBase
{
	public function logic()
	{
		// 从redis统计
		$users = 0;
		$redis = $this->redis('status');
		if(is_array($keys = $redis->keys('uidInServer*')) && count($keys) > 0)
		{
			foreach($keys as $key)
			{
				$users += (int)$redis->scard($key);
			}
		}
		$this->deinitRedis('status');

		// 写入数据库
		$data = [
			'users' => $users,
			'create_at' => time(),
		];
		$mysql = $this->mysql('logs');
        $sql = "INSERT INTO d_user_playing (`users`, `create_at`) VALUES ('{$data['users']}', '{$data['create_at']}')";
        $mysql->query($sql);
	}
}